{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e25572af",
   "metadata": {},
   "source": [
    "# 数据的读写\n",
    "\n",
    "Pandas模块能够读写很多格式的数据，如CSV、EXCEL、SQL、JSON、HTML,SAS，PICKLE等。在Pandas中，文件通常使用类似`pd.read_xxx()`形式的函数进行读取，并使用类似`.to_xxx()`形式的方法进行写入。\n",
    "\n",
    "例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6f16ab2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "095aa7e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e3dbbe2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randn(3, 5),\n",
    "                  index=list(\"abc\"),\n",
    "                  columns=list(\"ABCDE\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3610087b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>-0.020062</td>\n",
       "      <td>-0.599706</td>\n",
       "      <td>0.277161</td>\n",
       "      <td>0.080379</td>\n",
       "      <td>-0.646617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>1.438798</td>\n",
       "      <td>-1.000701</td>\n",
       "      <td>0.091833</td>\n",
       "      <td>-0.112943</td>\n",
       "      <td>0.860176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>-0.171525</td>\n",
       "      <td>1.093374</td>\n",
       "      <td>0.501429</td>\n",
       "      <td>-1.123508</td>\n",
       "      <td>-0.930662</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "a -0.020062 -0.599706  0.277161  0.080379 -0.646617\n",
       "b  1.438798 -1.000701  0.091833 -0.112943  0.860176\n",
       "c -0.171525  1.093374  0.501429 -1.123508 -0.930662"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bef4bddc",
   "metadata": {},
   "source": [
    "可以使用.to_csv()方法将它保存为CSV文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "60e60334",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"foo.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef6c4e32",
   "metadata": {},
   "source": [
    "CSV文件可以用pd.read_csv()函数读取："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f7f071fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>-0.020062</td>\n",
       "      <td>-0.599706</td>\n",
       "      <td>0.277161</td>\n",
       "      <td>0.080379</td>\n",
       "      <td>-0.646617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>1.438798</td>\n",
       "      <td>-1.000701</td>\n",
       "      <td>0.091833</td>\n",
       "      <td>-0.112943</td>\n",
       "      <td>0.860176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c</td>\n",
       "      <td>-0.171525</td>\n",
       "      <td>1.093374</td>\n",
       "      <td>0.501429</td>\n",
       "      <td>-1.123508</td>\n",
       "      <td>-0.930662</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Unnamed: 0         A         B         C         D         E\n",
       "0          a -0.020062 -0.599706  0.277161  0.080379 -0.646617\n",
       "1          b  1.438798 -1.000701  0.091833 -0.112943  0.860176\n",
       "2          c -0.171525  1.093374  0.501429 -1.123508 -0.930662"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"foo.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96f3c8be",
   "metadata": {},
   "source": [
    "与原来的DataFrame对象相比，读取到的对象多了Unnamed:0的列，它是原来DataFrame对象的行标记。在保存的时候，Pandas默认会保存行标记和列标记的值；在读取时，Pandas并不知道这一列是行标记，所以会将其当做普通的数据列读取出来。\n",
    "\n",
    "为了解决这个问题，可以在读取时，使用参数index_col指定行标记所在的列："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7853ebd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>-0.020062</td>\n",
       "      <td>-0.599706</td>\n",
       "      <td>0.277161</td>\n",
       "      <td>0.080379</td>\n",
       "      <td>-0.646617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>1.438798</td>\n",
       "      <td>-1.000701</td>\n",
       "      <td>0.091833</td>\n",
       "      <td>-0.112943</td>\n",
       "      <td>0.860176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>-0.171525</td>\n",
       "      <td>1.093374</td>\n",
       "      <td>0.501429</td>\n",
       "      <td>-1.123508</td>\n",
       "      <td>-0.930662</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "a -0.020062 -0.599706  0.277161  0.080379 -0.646617\n",
       "b  1.438798 -1.000701  0.091833 -0.112943  0.860176\n",
       "c -0.171525  1.093374  0.501429 -1.123508 -0.930662"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"foo.csv\", index_col=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67dc9786",
   "metadata": {},
   "source": [
    "也可以在保存的时候，忽略行标记："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "26b25445",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"foo.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "73dfbb8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.020062</td>\n",
       "      <td>-0.599706</td>\n",
       "      <td>0.277161</td>\n",
       "      <td>0.080379</td>\n",
       "      <td>-0.646617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.438798</td>\n",
       "      <td>-1.000701</td>\n",
       "      <td>0.091833</td>\n",
       "      <td>-0.112943</td>\n",
       "      <td>0.860176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.171525</td>\n",
       "      <td>1.093374</td>\n",
       "      <td>0.501429</td>\n",
       "      <td>-1.123508</td>\n",
       "      <td>-0.930662</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "0 -0.020062 -0.599706  0.277161  0.080379 -0.646617\n",
       "1  1.438798 -1.000701  0.091833 -0.112943  0.860176\n",
       "2 -0.171525  1.093374  0.501429 -1.123508 -0.930662"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"foo.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1086ab1c",
   "metadata": {},
   "source": [
    "其它类型文件的读写方法类似，在具体参数上可能会有一些不同，常用的方法有：\n",
    "- pandas.read_csv()函数，.to_csv()方法：CSV格式，逗号分割的文本格式。\n",
    "- pandas.read_json()函数，.to_json()方法：JSON格式。\n",
    "- pandas.read_html()函数，.to_html()方法：HTML网页上的表格。\n",
    "- pandas.read_excel()函数，.to_excel()方法：EXCEL格式的表格。\n",
    "\n",
    "值得注意的是，读写EXCEL时，需要调用一些额外的模块，如xlrd、xlwt、openpyxl等，如果系统没有按照这些模块，需要额外通过pip或者conda安装。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "12b8fcdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "%rm foo.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f1ef51d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
